package edu.mis.modules.kmye;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import com.jfinal.render.Render;

import edu.mis.model.AccountCaption;
import edu.mis.model.Kmye;
import edu.mis.model.Voucheritem;

public class KmyeController extends Controller {
	public void index() {
		render("kmqcye.html");
	}

	/**
	 * 判断是否已经进行过设置
	 */
	public void isNull() {
		List<Kmye> data = Kmye.dao.find("select * from kmye limit 1");
		if (data.size() == 0) {
			setAttr("isNull", true);
		} else {
			setAttr("isNull", false);
		}
		renderJson();
	}

	/**
	 * 获取ac列表
	 */
	public void list() {
		List<AccountCaption> data = AccountCaption.dao.find(
				"SELECT captionId,name,balanceDirection FROM account_caption where captionId like '__%' ORDER BY captionId ASC");
		setAttr("rows", data);
		renderJson();
	}

	/**
	 * 获取kmye列表
	 */
	public void list2() {
		List<Kmye> data = Kmye.dao.find(
				"SELECT k.captionId,k.balanceDirection,k.openingBalance,ac.name FROM kmye k LEFT JOIN account_caption ac ON k.captionId=ac.captionId where isInit=1");
		setAttr("rows", data);
		renderJson();
	}

	/**
	 * 获取启用时间
	 */
	public void getInitTime() {
		List<Kmye> data = Kmye.dao.find("SELECT date FROM kmye where isInit=1 limit 1");
		java.sql.Date date = data.get(0).get("date");
		java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
		sformat.applyPattern("yyyy");
		setAttr("date", date);
		setAttr("year", sformat.format(date));
		renderJson();
	}
	
	/**
	 * 获取上次关账的时间
	 */
	public void getLastCloseTime() {
		List<Kmye> data = Kmye.dao.find("select date from kmye where isInit = 0 order by date desc limit 1");
		java.sql.Date date = data.get(0).get("date");
		setAttr("date", date);
		renderJson();
	}

	/**
	 * 获取可查看总账的年份列表
	 */
	public void getGLTime() {
		List<Kmye> data = Kmye.dao.find("SELECT date FROM kmye where isInit=0 group by year(date)");
		int len = data.size();
		for (int i = 0; i < len; i++) {
			java.sql.Date date = data.get(i).get("date");
			java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
			sformat.applyPattern("yyyy");
			data.get(i).set("date", sformat.format(date));
		}
		render(new JsonRender(data));
	}
	
	/**
	 * 获取可查看明细账的年月列表
	 */
	public void getSLTime() {
		List<Kmye> data = Kmye.dao.find("SELECT date FROM kmye where isInit=0 group by date");
		int len = data.size();
		for (int i = 0; i < len; i++) {
			java.sql.Date date = data.get(i).get("date");
			java.text.SimpleDateFormat sformat = new java.text.SimpleDateFormat();
			sformat.applyPattern("yyyy-MM");
			data.get(i).set("date", sformat.format(date));
		}
		render(new JsonRender(data));
	}

	/**
	 * 本月关账
	 * @throws Exception
	 */
	public void close() throws Exception {
		String data=new KmyeService().close();
		renderJson(data);
	}

	/**
	 * 保存科目余额设置
	 * 
	 * @throws Exception
	 */
	public void save() throws Exception {
		String jsonString = new String(getPara("kmqcye"));
		JSONArray array = JSON.parseArray(jsonString);
		int len = array.size();
		java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
		for (int i = 0; i < len; i++) {
			Kmye k = new Kmye();
			JSONObject temp = array.getJSONObject(i);
			k.setCaptionId(temp.getInteger("captionId"));
			k.setDate(date);
			if (temp.getDouble("openingBalance") == null) {
				k.setOpeningBalance(0.0);
			} else {
				k.setOpeningBalance(temp.getDouble("openingBalance"));
			}

			k.setBalanceDirection(temp.getString("balanceDirection"));
			k.setJffs(0.0);
			k.setDffs(0.0);
			k.setLjjf(0.0);
			k.setLjdf(0.0);
			k.setIsInit(1);
			if (k.save()) {
				setAttr("success", true);
				setAttr("msg", "设置科目期初成功!");
			} else {
				setAttr("success", false);
				setAttr("msg", "设置科目期初失败!");
			}
		}
		renderJson();
	}
}
